package com.syxp.pda.core.command;

import java.util.Collection;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import com.syxp.pda.core.bean.FieldBean;
import com.syxp.pda.core.util.Operation;

public class FieldCommand implements CriteriaCommand{

	private FieldBean field ;

	public FieldCommand(){
	}
	
	public FieldCommand(FieldBean field){
		this.field = field ;
	}
	@Override
	public Criteria execute(Criteria criteria) {
		
		if(StringUtils.equals(this.field.getOperation(), Operation.LT)){
			criteria.add(Restrictions.lt(field.getProperty(), field.getValue())) ;
		}else if(StringUtils.equals(this.field.getOperation(), Operation.LE)) {
			criteria.add(Restrictions.le(field.getProperty(), field.getValue())) ;
		}else if(StringUtils.equals(this.field.getOperation(), Operation.GT)){
			criteria.add(Restrictions.gt(field.getProperty(), field.getValue())) ;
		}else if(StringUtils.equals(this.field.getOperation(), Operation.GE)){
			criteria.add(Restrictions.ge(field.getProperty(), field.getValue())) ;
		}else if(StringUtils.equals(this.field.getOperation(), Operation.EQUAL)){
			criteria.add(Restrictions.eq(field.getProperty(), field.getValue())) ;
		}else if(StringUtils.equals(this.field.getOperation(), Operation.NEQUAL)){
			criteria.add(Restrictions.ne(field.getProperty(), field.getValue())) ;
		}else if(StringUtils.equals(this.field.getOperation(), Operation.LIKE)){
			criteria.add(Restrictions.like(field.getProperty(), field.getValue())) ;
		}else if(StringUtils.equals(this.field.getOperation(), Operation.IN)){
			criteria.add(Restrictions.in(field.getProperty(), (Collection)field.getValue())) ;
		}else if(StringUtils.equals(this.field.getOperation(), Operation.ISEMPTY)){
			criteria.add(Restrictions.isEmpty(field.getProperty())) ;
		}else if(StringUtils.equals(this.field.getOperation(), Operation.ISNOTEMPTY)) {
			criteria.add(Restrictions.isNotEmpty(field.getProperty())) ;
		}else if(StringUtils.equals(this.field.getOperation(), Operation.ISNULL)){
			criteria.add(Restrictions.isNull(field.getProperty())) ;
		}else if(StringUtils.equals(this.field.getOperation(), Operation.ISNOTNULL)){
			criteria.add(Restrictions.isNotNull(field.getProperty())) ;
		}
		
		return criteria;
	}

}
